package com.sgolub.commanndsonline.service;


import org.apache.log4j.Logger;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
 
/**
 * Class providing convenience method for abstract thumbnail generators
 */
public class AbstractThumbnailGenerator {
 
	private final static Logger log = Logger.getLogger(AbstractThumbnailGenerator.class);
 
	/**
     * Save an image as a JPEG file on disk.
     *
     * @param image     The raw image to save.
     * @param fileOut   The location where you want to save the file.
     *
     * @return true if successful, false if unsuccessful.
	 * @throws java.io.IOException if something goes wrong closing the stream
     */
    public boolean saveImageAsJPEG(BufferedImage image, File fileOut) throws IOException {
        OutputStream streamOut = null;
        boolean      bSuccess = false;
 
        try {
            streamOut = new FileOutputStream(fileOut);
            
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(streamOut);
//            encoder.encode(image);
            
            ImageIO.write(image, "jpeg", streamOut);
            
            bSuccess = true;
        }
        catch (Throwable t) {
            log.warn("Files.saveImageAsJPEG(" + fileOut + "): " + t, t);
        }
        finally {
            if (streamOut != null) {
				streamOut.close();
			}
        }
 
        return bSuccess;
    }
 
 
}